$(function () {
    Time();
    load();
})

function fh() {
    localStorage.removeItem("mManufactureSH");
    location.href = "../../wen_zhi_yi/mManufactureSH.html";
}

function Time() {
    var time = new Date();
    var y = time.getFullYear();
    var mon = time.getMonth() + 1; //0-11
    var d = time.getDate();
    var h = time.getHours();
    var m = time.getMinutes();
    var s = time.getSeconds();
    $("#date").val(y + "-" + mon + "-" + d + " " + h + ":" + m + ":" + s);
}

let list = [];

function load() {
    let user = JSON.parse(localStorage.getItem("user"));
    $("#rname").val(user.rname);
    let mManufactureSH = JSON.parse(localStorage.getItem("mManufactureSH"));
    $("#manufactureId").text(mManufactureSH.manufactureid);
    $("#productId").text(mManufactureSH.productid);
    $("#productName").text(mManufactureSH.productname);
    $("#amount").text(mManufactureSH.amount);
    $("#designer").val(mManufactureSH.designer);
    $("#gongshi").text(mManufactureSH.labourcostpricesum);
    $("#wuliao").text(mManufactureSH.modulecostpricesum);
    $("#name").text(mManufactureSH.register);
    $("#datetime").text(mManufactureSH.registertime);
    $("#productDescribe").text(mManufactureSH.productdescribe);
    $("#remark").text(mManufactureSH.remark);

    $.post("/Mmanufacture.do/mManufactureSHCha", {id: mManufactureSH.id}, function (sdata) {
        $("#mapID").text(sdata.manufactureid);
        let str = " <tr>\n" +
            "          <th>序号</th>\n" +
            "          <th>工序名称</th>\n" +
            "          <th>工序编号</th>\n" +
            "          <th>工时数（h）</th>\n" +
            "          <th>单位工时成本（元）</th>\n" +
            "          <th>工时成本小计(元)</th>\n" +
            "          <th>物料成本小计</th>\n" +
            "          <th>审核物料</th>+\n" +
            "      </tr>\n";
        list = sdata.MprocedureList;
        $.each(sdata.MprocedureList, function (index, item) {
            let s = JSON.stringify(item.MpLists);
            str += "      <tr>\n" +
                "          <td>" + item.detailsnumber + "</td>\n" +
                "          <td>" + item.procedurename + "</td>\n" +
                "          <td>" + item.procedureid + "</td>\n" +
                "          <td><span class='labourhouramount'>" + item.labourhouramount + "</span> </td>\n" +
                "          <td><span class='costprice'>" + item.costprice + "</span></td>\n" +
                "          <td><span class='subtotal'>"+ item.subtotal + "</span> </td>\n" +
                "          <td><span class='modulesubtotal'>" + item.modulesubtotal + "</span> </td>\n" +
                "          <td><a href=\"#\" onclick='XQ03(" + s + ",\"" + item.procedurename + "\")'>审核物料</a> </td>\n" +
                "      </tr>";
            $("#name").val(item.register);
            $("#datetime").val(item.registertime);
        })
        $("#tableSH").html(str);
    })
}

function Submit() {
    let tf= $("[name='zt']:checked").val();
    let duixiang = {
        "checker": $("#rname").val(),
        "checktime": $("#date").val(),
        "checktag": $("[name='zt']:checked").val(),
        "remark": $("#remark").val(),
        "manufactureid": $("#manufactureId").text()
    };
    let user = JSON.parse(localStorage.getItem("user"));
     let sPay ={
         "storer":user.account,
         "reasonexact":"生产领料",
         "costpricesum": $("#wuliao").text(),
         "register": user.account,
         "registertime": $("#date").val(),
         "checktag": 'S001-0',
         "attemper": user.account,
         "attempertime": $("#date").val(),
         "storetag":'K002-2' };

    let mManufacture = JSON.stringify(duixiang);
    let sPays = JSON.stringify(sPay);
    let slist = JSON.stringify(list);

    $.post("/Mmanufacture.do/updateWZYSHZT", {mManufacture:mManufacture}, function (sd) {
        if (tf=="S001-1"){
            $.post("/Mmanufacture.do/MmanufactureSpaySpaydetails", {spay:sPays,slist:slist}, function (sd) {})
        }
        $("#tishi").text("审核成功！")
        $("#tishi").css("color", "lime")
        $("#staticBackdrop").modal("show");
    })
}

function hh() {
    localStorage.removeItem("mManufactureSH");
    location.href = "../../wen_zhi_yi/mManufactureSH.html";
}

function XQ03(op, dname) {
    $("#XQModal").modal("show");
    $("#dname").text(dname);
    let sstr = " <tr>\n" +
        "          <th>序号</th>\n" +
        "          <th>物料名称</th>\n" +
        "          <th>物料编号</th>\n" +
        "          <th>本工序数量</th>\n" +
        "          <th>单价（元）</th>\n" +
        "          <th>小计(元)</th>\n" +
        "      </tr>\n";
    $.each(op, function (index, item) {
        sstr += "      <tr>\n" +
            "          <td>" + item.id + "</td>\n" +
            "          <td>" + item.productname + "</td>\n" +
            "          <td>" + item.productid + "</td>\n" +
            "          <td>" + item.amount + "</td>\n" +
            "          <td>" + item.costprice + "</td>\n" +
            "          <td>" + item.subtotal + "</td>\n" +
            "      </tr>";
    })
    $("#tm01").html(sstr);
}



